package com.tianda.his.modules.erp.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tianda.his.commons.utils.LoginUtils;
import com.tianda.his.commons.vo.AjaxResult;
import com.tianda.his.modules.erp.dto.PruducerDto;
import com.tianda.his.modules.erp.entity.StockProducer;
import com.tianda.his.modules.erp.service.StockProducerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

/**
 * 生成厂家管理
 */
@RestController
public class StockProducerController {

    @Autowired
    private StockProducerService stockProducerService;

    /**
     * 分页查询
     * @param pruducerDto
     * @return
     */
    @GetMapping("/erp/producter/listProducterForPage")
    public AjaxResult listProducterForPage(PruducerDto pruducerDto) {
        Page<StockProducer> pageResult = stockProducerService.listProducterForPage(pruducerDto);
        return AjaxResult.success(pageResult.getRecords(), pageResult.getTotal());
    }

    /**
     * 新增生产厂家
     * @param stockProducer
     * @return
     */
    @PostMapping("/erp/producter/addProducter")
    public AjaxResult addProducter(StockProducer stockProducer) {
        stockProducer.setCreateTime(new Date());
        stockProducer.setCreateBy(LoginUtils.getLoginUser().getUserName());

        stockProducerService.save(stockProducer);
        return AjaxResult.success();

    }

    /**
     * 修改生产厂家
     * @param stockProducer
     * @return
     */
    @PutMapping("/erp/producter/updateProducter")
    public AjaxResult updateProducter(StockProducer stockProducer) {
        stockProducer.setUpdateTime(new Date());
        stockProducer.setUpdateBy(LoginUtils.getLoginUser().getUserName());

        stockProducerService.updateById(stockProducer);
        return AjaxResult.success();
    }

    /**
     * 根据id查询
     * @param producterId
     * @return
     */
    @GetMapping("/erp/producter/getProducterById/{producterId}")
    public AjaxResult getProducterById(@PathVariable("producterId") Long producterId) {
        return AjaxResult.success(stockProducerService.getById(producterId));
    }

    /**
     * 删除生产厂家
     * @param producterId
     * @return
     */
    @DeleteMapping("/erp/producter/deleteProducterByIds/{producterId}")
    public AjaxResult deleteProducterById(@PathVariable("producterId") Long producterId) {
        stockProducerService.removeById(producterId);
        return AjaxResult.success();
    }

    @GetMapping("/erp/producter/selectAllProducter")
    public AjaxResult selectAllProducter() {
        return AjaxResult.success(stockProducerService.list());
    }


}
